use tauri::plugin;
use tauri_plugin_http::reqwest;
// Learn more about Tauri commands at https://tauri.app/v1/guides/features/command
#[tauri::command]
fn greet(name: &str) -> String {
    format!("Hello, {}! You've been greeted from Rust!", name)
}

#[tauri::command]
async fn get_music_data() -> String {
    let url = "https://c.y.qq.com/qzone/fcg-bin/fcg_ucc_getcdinfo_byids_cp.fcg?utf8=1&onlysong=1&nosign=1&new_format=1&ctx=1&_=1622351815722&cv=4747474&ct=24&format=json&inCharset=utf-8&outCharset=utf-8&notice=0&platform=yqq.json&needNewCode=1&uin=1152921504779438588&g_tk_new_20200303=1317207099&g_tk=1317207099&disstid=3617227478&json=1&type=1";
    let client = reqwest::Client::new();
    client
        .get(url)
        .header("referer", "https://c.y.qq.com/")
        .header("host", "c.y.qq.com")
        .send()
        .await
        .unwrap()
        .text()
        .await
        .unwrap()
}
#[tauri::command]
async fn get_music_url(mid: String, media_mid: String, s: String, e: String) -> String {
    let file_name = format!("{}{}{}{}", s, mid, media_mid, e);
    let url =format!("https://u.y.qq.com/cgi-bin/musicu.fcg?format=json&sign=zzannc1o6o9b4i971602f3554385022046ab796512b7012&data=%7B%22req_0%22:%7B%22module%22:%22vkey.GetVkeyServer%22,%22method%22:%22CgiGetVkey%22,%22param%22:%7B%22filename%22:[%22{}%22],%22guid%22:%221429839143%22,%22songmid%22:[%22{}%22],%22songtype%22:[0],%22uin%22:%221429839143%22,%22loginflag%22:1,%22platform%22:%2220%22%7D%7D,%22comm%22:%7B%22uin%22:%2218585073516%22,%22format%22:%22json%22,%22ct%22:24,%22cv%22:0%7D%7D",file_name,mid);
    let client = reqwest::Client::new();
    client.get(url).send().await.unwrap().text().await.unwrap()
}

#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
    tauri::Builder::default()
        .plugin(tauri_plugin_http::init())
        .plugin(tauri_plugin_shell::init())
        .invoke_handler(tauri::generate_handler![
            greet,
            get_music_data,
            get_music_url
        ])
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}
